package com.neusoft.bizcore.activiti.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.activiti.dto.PendingDataDTO;
import com.neusoft.bizcore.activiti.service.PendingDataService;
import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "工作流中间表管理")
@RestController
@RequestMapping(value = "/api/w/act/pending")
public class ActPendingDataController {
    @Autowired
    private PendingDataService pendingDataService;

    @ApiOperation("查询所有业务数据处理")
    @GetMapping
    public ResultListDTO<PendingDataDTO> index(final Searchable searchable, final UserBean user) {
        final List<PendingDataDTO> DTOs = this.pendingDataService.index(searchable);
        final ResultListDTO<PendingDataDTO> result = ResultListDTO.success(DTOs);
        return result;
    }

    @ApiOperation("分页查询所有业务数据处理")
    @GetMapping("/p")
    public PageResultDTO<PendingDataDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.pendingDataService.search(pageable, searchable));
    }

    @ApiOperation("创建业务数据处理")
    @PostMapping
    public ResultDTO<PendingDataDTO> create(@RequestBody final PendingDataDTO dto) {
        final PendingDataDTO result = this.pendingDataService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("业务数据处理详情")
    @GetMapping("/{id}")
    public ResultDTO<PendingDataDTO> details(@PathVariable final Long id) {
        final PendingDataDTO dto = this.pendingDataService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("根据workNum和nodeId删除业务数据处理")
    @DeleteMapping("/delete/{workNum}/{nodeId}")
    public ResultDTO<Void> deleteByWorkNumAndTaskId(@PathVariable final String workNum,
            @PathVariable final String nodeId) {
        this.pendingDataService.deleteByWorkNumAndNodeId(workNum, nodeId);
        return ResultDTO.success();
    }

    @ApiOperation("根据workNum删除业务数据处理")
    @DeleteMapping("/delete/{workNum}")
    public ResultDTO<Void> deleteByWorkNum(@PathVariable final String workNum) {
        this.pendingDataService.deleteByWorkNum(workNum);
        return ResultDTO.success();
    }

    @ApiOperation("删除业务数据处理")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.pendingDataService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation("修改业务数据处理")
    @PutMapping("/{id}")
    public ResultDTO<PendingDataDTO> update(@PathVariable final Long id,
            @RequestBody final PendingDataDTO dto) {
        final PendingDataDTO result = this.pendingDataService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("手动处理发生异常的业务数据")
    @PutMapping("/dealData/{processInstanceId}")
    public ResultDTO<String> dealData(@PathVariable final String processInstanceId) {
        final String result = this.pendingDataService.dealData(processInstanceId);
        return ResultDTO.success(result);
    }
}
